package edu.umassd.visual;

import java.awt.Color;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import edu.umassd.visual.components.Displayable;
import edu.umassd.visual.util.Rectangle;

@Component
public class RADDACLDisplay extends Display implements MouseWheelListener
{
	private static final long serialVersionUID = 8846609201310306570L;

	@Autowired
	private Map<String, Displayable<? super Display>> map;

	private Double zoom = 100.0;

	private int mouseXLast;
	private int mouseYLast;

	// @Autowired
	// private DataPoints points;

	@Autowired
	private View view;

	@Override
	public void setup()
	{
		view.setDataSetBounds(new Rectangle(1000, 1000));
		addMouseWheelListener(this);
	}

	@Override
	public void draw()
	{
		background(Color.BLACK);

		// data
		map.get("data").display(this);

		// overlays
		map.get("screensize").display(this);
		map.get("zoom").display(this);
		map.get("coords").display(this);
	}

	@Override
	public void mousePressed()
	{
		mouseXLast = mouseX;
		mouseYLast = mouseY;
	}

	@Override
	public void mouseDragged()
	{
		int x = mouseX;
		int y = mouseY;

		view.pan(-(double) (x - mouseXLast), -(double) (y - mouseYLast));

		mouseXLast = x;
		mouseYLast = y;
	}

	@Override
	public void mouseClicked()
	{
		// double x = view.canvasToDataX(mouseX);
		// double y = view.canvasToDataY(mouseY);
		//
		// points.addPoint(x, y);
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e)
	{
		zoom -= e.getPreciseWheelRotation();

		if (zoom < 100.0)
		{
			zoom = 100.0;
		}
		else if (zoom > 400.0)
		{
			zoom = 400.0;
		}

		view.zoom(zoom);
	}

	/**
	 * The current zoom level. Represents the amount of the input space actually
	 * visible, where 1.0 is the entire space and 0.0 is no space.
	 * 
	 * @return the current amount of zoom.
	 */
	public Double getZoom()
	{
		return zoom;
	}
}
